﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../global/head.h"
#include "../xos/head.h"
#include "thread_impl.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos
{
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    static thread_impl::POOL * pool_ptr = 0;

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    thread_impl::thread_impl()
    {
        m_thread_fun = 0;
        m_lpData = 0;
        m_hThread = 0;
    }

    thread_impl::~thread_impl()
    {
        term();
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int thread_impl::get_addr( POOL *** pppPool, LIST *** pppList )
    {
        int ret = 0;
        if( pppPool )
        {
            *pppPool = &pool_ptr;
        }
        if( pppList )
        {
            *pppList = 0;
        }
        return ret;
    }

    int thread_impl::init()
    {
        int nRet = 0;
        return nRet;
    }

    int thread_impl::term()
    {
        int nRet = 0;

        if( m_hThread )
        {
            CloseHandle( m_hThread );
            m_hThread = 0;
        }

        m_thread_fun = 0;
        m_lpData = 0;

        return nRet;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    DWORD thread_impl::thread_proc( LPVOID lpData )
    {
        thread_impl & impl = *( thread_impl * )lpData;
        impl.run();
        return 0;
    }

    int thread_impl::run()
    {
        int nRet = 0;
        m_thread_fun( m_lpData );
        return nRet;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////
    // 
    // create thread
    // 
    int thread_impl::create( void * ( * f )( void * ), void * lpData )
    {
        int nRet = 0;

        m_thread_fun = f;
        m_lpData = lpData;

        m_hThread = CreateThread( 
            0,
            0,
            thread_proc,
            this,
            0,
            0 );

        if( !m_hThread )
        {
            nRet = 1;
        }

        return nRet;
    }

    // 
    // wait thread to terminate..
    // 
    int thread_impl::wait()
    {
        int nRet = 0;

        if( m_hThread )
        {
            WaitForSingleObject( m_hThread, INFINITE );
            CloseHandle( m_hThread );
            m_hThread = 0;
        }

        return nRet;
    }
} // xos
